<?php

namespace app\common\model;

use think\Model;

/**用户刷题报告 */
class UserQuestionReport extends Model
{
    // 表名
    protected $name = 'user_question_report';

    public function saveUserQuestionReport($user_id, $param)
    {

        $info = self::where('user_id', $user_id)->where('question_group_id', $param['question_group_id'])->find();
        if (empty($info)) {
            $data = [
                'user_id' => $user_id,
                'question_group_id' => $param['question_group_id'],
                'adduptime' => $param['adduptime'],
                'right_count' => $param['right_count'],
                'wrong_count' => $param['wrong_count'],
                'blank_count' => $param['blank_count'],
                'createtime' => time(),
            ];
            $res = self::insert($data);
            if (!$res) {
                fail('提交失败');
            }
        }
        # 更新 把之前数据累加
        else {
            $data = [
                'right_count' => $param['right_count'],
                'wrong_count' => $param['wrong_count'],
                'blank_count' => $param['blank_count'],
                'adduptime' => $info->adduptime + $param['adduptime'],
                'updatetime' => time(),
            ];
            $res = self::where('user_id', $user_id)->where('question_group_id', $param['question_group_id'])->update($data);
            if (!$res) {
                fail('提交失败');
            }
        }
        return [
            'adduptime' => $data['adduptime'],
            'right_count' => $data['right_count'],
            'wrong_count' => $data['wrong_count'],
            'blank_count' => $data['blank_count'],
        ];
    }
}
